package com.etc.jdk8.stream;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: knowno
 * @create: 2024-09-14 16:07
 * @Version 1.0
 **/
public class TestEmployee {
    public static void main(String[] args) {

        //构造一些员工对象
        Employee employee1 = new Employee(1,"a",23);
        Employee employee2 = new Employee(3,"b",24);
        Employee employee3 = new Employee(2,"c",26);
        Employee employee4 = new Employee(4,"d",28);
        Employee employee5 = new Employee(5,"e",21);
        Employee employee6 = new Employee(1,"a",23);

        //创建一个集合
        List<Employee> list = Arrays.asList(employee1,employee2,employee3,employee4,employee5,employee6);

        System.out.println("******1  list.stream().distinct()**********");
        //1 创建流对象--> 去除重复元素
        list.stream().distinct().forEach(System.out::println);

        System.out.println("******2  list.stream().sorted(**********");
        //2排序
        list.stream().sorted((o1,o2)->{
            //int compare(T o1, T o2);
            return o1.getAge() - o2.getAge();
        }).forEach(System.out::println);
        System.out.println("******2  list.stream().sorted(**********");
        list.stream().sorted(Comparator.comparing(Employee::getAge)).forEach(System.out::println);

        System.out.println("******3 list.stream().filter**********");
        //3 过滤
        list.stream().filter((Employee employee)->employee.getAge()>25).forEach(System.out::println);

        //4 截断数据-》分页显示部分记录
        System.out.println("******4 list.stream().filter limit(2) **********");
        list.stream().filter((Employee employee)->employee.getAge()>23).limit(2).forEach(System.out::println);


        //5 展示统计数据
        System.out.println("******5  list.stream().mapToInt(p -> p.getAge()).summaryStatistics() **********");
        IntSummaryStatistics intSummaryStatistics = list.stream().mapToInt(p -> p.getAge()).summaryStatistics();
        System.out.println(intSummaryStatistics);

        //6 转换为 list->Stream->List
        System.out.println("******6  list.stream().map(p -> p.getName().toUpperCase()).collect(Collectors.toList())**********");
        List<String> collect = list.stream().map(p -> p.getName().toUpperCase()).collect(Collectors.toList());
        collect.forEach(System.out::println);
    }
}
